const axios = require('axios');

(async () => {
  console.log('🔍 调试可获得勋章筛选问题');
  
  try {
    // 1. 登录用户
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'admin@campus.com',
      password: 'admin123'
    });
    
    const authToken = loginResponse.data.token;
    const userId = loginResponse.data.user.id;
    console.log(`✅ 用户登录成功: ID=${userId}`);
    
    // 2. 获取可获得勋章详细信息
    const availableBadgesResponse = await axios.get('http://localhost:3000/api/badges?filter=available', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    console.log(`🔓 可获得勋章数量: ${availableBadgesResponse.data.badges.length}`);
    
    // 3. 分析每个勋章的状态
    console.log('\n📋 可获得勋章列表分析:');
    
    let actualAvailable = 0;
    let incorrectlyIncluded = 0;
    
    availableBadgesResponse.data.badges.forEach((badge, index) => {
      const isReallyAvailable = badge.status === 'available' && !badge.isOwned;
      const isIncorrectlyIncluded = badge.status === 'minted' || badge.isOwned;
      
      if (isReallyAvailable) {
        actualAvailable++;
      } else if (isIncorrectlyIncluded) {
        incorrectlyIncluded++;
        console.log(`❌ 错误包含的勋章 ${index + 1}:`);
        console.log(`   名称: ${badge.badge_name}`);
        console.log(`   状态: ${badge.status}`);
        console.log(`   用户ID: ${badge.user_id}`);
        console.log(`   是否拥有: ${badge.isOwned}`);
        console.log(`   活动ID: ${badge.activity_id}`);
        console.log('');
      }
    });
    
    console.log(`✅ 真正可获得的勋章: ${actualAvailable}`);
    console.log(`❌ 错误包含的勋章: ${incorrectlyIncluded}`);
    
    // 4. 获取用户已获得的勋章做对比
    const ownedBadgesResponse = await axios.get('http://localhost:3000/api/badges?filter=owned', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    
    console.log(`\n🎖️ 用户已获得勋章: ${ownedBadgesResponse.data.badges.length}`);
    
    // 5. 获取所有活动数量
    const activitiesResponse = await axios.get('http://localhost:3000/api/activities');
    console.log(`📅 系统活动总数: ${activitiesResponse.data.activities.length}`);
    
    // 6. 计算预期值
    const expectedAvailable = activitiesResponse.data.activities.length - ownedBadgesResponse.data.badges.length;
    console.log(`\n🎯 预期可获得勋章数: ${activitiesResponse.data.activities.length} - ${ownedBadgesResponse.data.badges.length} = ${expectedAvailable}`);
    console.log(`📊 实际可获得勋章数: ${availableBadgesResponse.data.badges.length}`);
    
    if (expectedAvailable === availableBadgesResponse.data.badges.length) {
      console.log('✅ 可获得勋章数量正确');
    } else {
      console.log('❌ 可获得勋章数量不正确');
    }
    
    // 7. 检查是否有活动ID重复
    console.log('\n🔍 检查活动ID重复情况:');
    const activityIds = availableBadgesResponse.data.badges.map(badge => badge.activity_id);
    const uniqueActivityIds = [...new Set(activityIds)];
    
    console.log(`勋章中的活动ID数量: ${activityIds.length}`);
    console.log(`去重后的活动ID数量: ${uniqueActivityIds.length}`);
    
    if (activityIds.length !== uniqueActivityIds.length) {
      console.log('❌ 存在重复的活动ID！');
      // 找出重复的ID
      const duplicates = activityIds.filter((id, index) => activityIds.indexOf(id) !== index);
      console.log('重复的活动ID:', [...new Set(duplicates)]);
    } else {
      console.log('✅ 无重复活动ID');
    }
    
  } catch (error) {
    console.error('❌ 调试失败:', error.message);
    if (error.response) {
      console.error('详细错误:', error.response.data);
    }
  }
})(); 